<?php
if (!session_start()) session_start();
if (!isset($_SESSION['kiemtradangnhapthinguyet']) || $_SESSION['kiemtradangnhapthinguyet'] != 1) {
    header('location: ../../../index.php');
    die();
}
$table  = "menu_relationship";
$id     = $_GET['id'];
$kq     = $qt->get_by_thutu($table, $id);
$record = mysql_fetch_assoc($kq);
$list_menu_root = $qt->get_by_order('menu_domain', -1, 1);
if (isset($_POST['ok'])) {
    $id   = $_POST['id'];
    $bien = $_POST;
    unset($bien['ok']);
    if($bien['domain_id'] != 0 && $bien['menu_id'] != 0) {
        $res = $qt->updateObject1($table, $bien, "id='$id'");
        if ($res) {
            echo "<script>alert('Sửa dữ liệu thành công !!!');history.go(-2);</script>";
        }
    } else {
        echo '<script>alert("Hãy điền đẩy đủ thông tin");history.go(-1);</script>';
    }
}
?>
<script type="text/javascript">
    function isTest() {
        if (document.getElementById("ten_vi").value == "") {
            alert("You have not entered enough information.");
            return false;
        }
        if (document.getElementById("ten_en").value == "") {
            alert("You have not entered enough information.");
            return false;
        }

        return true;
    }
    /**
     * Lấy danh sách menu trong bảng menu_relationship bởi id của domain_id
     */
    function getMenuRelationship() {
        var id = document.getElementById("domain_id").value;
        $("#parent_id").load("librarys/incs/getloai.php?table=menu_relationship&loai=domain_id&id=" + id);
    }
    $(document).ready(function(){
        $('#menu_id').change(function() {
            var ele = $('option:selected', this);
            var value = ele.attr('data-value');
            $('input[name=table_name]').val(value);
        });
    })
</script>
<div class="dangnhap_title">
    <a href="index.php" style="color:#F8991D;" title="Trang chủ">Trang chủ</a>
    <span style="font-size:9px;">>></span>
    <a href="index.php?p=<?php echo $table; ?>_quanly" style="color:#F8991D;">Menu relationship</a>
</div>
<form method="post" action="index.php?p=<?php echo $table; ?>_sua" onSubmit="return isTest();">
    <input type="hidden" value="<?=$record['id']?>" name="id">
    <input type="hidden" name="table_name" value="<?=$record['table_name']?>" />
    <div class="dangnhap_col">
        <div class="dangnhap_col_title" style="font-size:22px; color:#0E6BBB; width: 90%;">
            Vietnamese's Information
        </div>
    </div>
    <div class="dangnhap_col">
        <div class="dangnhap_col_title">
            &nbsp; &nbsp; &nbsp;- Domain
        </div>
        <div class="dangnhap_col_input">
            <select name="domain_id" id="domain_id" onchange="getMenuRelationship()">
                <?php
                while ($row = mysql_fetch_assoc($list_menu_root)) {
                    echo '<option '.($record['domain_id'] == $row['id'] ? 'selected="selected"' : '' ).' value="'.$row['id'].'">'.$row['ten_vi'].'</option>';
                }
                ?>
            </select>
        </div>
    </div>
    <div class="dangnhap_col">
        <div class="dangnhap_col_title">
            &nbsp; &nbsp; &nbsp;- Danh mục cấp 1
        </div>
        <div class="dangnhap_col_input">
            <select name="parent_id" id="parent_id" style="width: 130px;">
                <option value="0">----------------------------</option>
                <?php
                $list_parent = $qt->get_by_order('menu_relationship', -1, 1, ' and parent_id=0');
                while($row1 = mysql_fetch_assoc($list_parent)) {
                    if($row1['id'] != $record['id']) {
                        echo '<option '.($record['parent_id'] == $row1['id']? 'selected="selected"' : "" ).' value="'.$row1['id'].'">'.$row1['ten_vi'].'</option>';
                    }
                }
                ?>
            </select>
        </div>
    </div>
    <div class="dangnhap_col">
        <div class="dangnhap_col_title">
            &nbsp; &nbsp; &nbsp;- Danh mục cấp 2
        </div>
        <div class="dangnhap_col_input">
            <select name="menu_id" id="menu_id" style="width: 130px;">
                <option value="0">----------------------------</option>
                <?php
                $qt->createMenuSelectBox($record['menu_id'], $record['table_name']);
                ?>
            </select>
        </div>
    </div>
    <div class="dangnhap_col">
        <div class="dangnhap_col_title">
            &nbsp; &nbsp; &nbsp;- Tên(*)
        </div>
        <div class="dangnhap_col_input">
            <input type="text" value="<?=$record['ten_vi']?>" name="ten_vi" id="ten_vi" style="width:520px; padding:0px;"/>
        </div>
    </div>
    <div class="dangnhap_col">
        <div class="dangnhap_col_title">
            &nbsp; &nbsp; &nbsp;- Mô tả
        </div>
        <div class="dangnhap_col_input">
            <input type="text" value="<?=$record['description']?>" name="description" id="description" style="width:520px; padding:0px;"/>
        </div>
    </div>
    <div class="dangnhap_col">
        <div class="dangnhap_col_title">
            &nbsp; &nbsp; &nbsp;- Hidden or Show:
        </div>
        <div class="dangnhap_col_input">
            <input type="radio" name="anhien" value="1" checked="checked"/>Show
            <input type="radio" name="anhien" value="0"/>Hidden
        </div>
    </div>
    <div class="dangnhap_col">
        <div class="dangnhap_col_input">
            <input type="submit" class="ok" border="0" name="ok" id="ok" value=""/>
            <a href="javascript:history.go(-1);"><img src="imgs/exit_button.png" border="0"/></a>
        </div>
    </div>
</form>